/********************************************************************************
  * @file     SD93F115B_tim.h
  * @author   TEST TEAM 													    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief   
  *           
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd
******************************************************************************/

/* Define to prevent recursive inclusion*/
#ifndef __SD93F115B_TIM_H
#define __SD93F115B_TIM_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes */
#include "SD93F115B.h"

/************The group of TIMER******************/

#define IS_TM_ALL_PERIPH(PERIPH) (((PERIPH) == TM0)||\
								  ((PERIPH) == TM1)||\
								  ((PERIPH) == TM2))
								  
								  
#define IS_TM_Reload_Value(Value) (((Value) >= 0)&&((Value) <= 0xFFFF))


/*****TIM Input Capture Init structure definition*********/
typedef struct
{

  uint16_t TM_CCPChannel;         

  uint16_t TM_CCPMode;            

  uint16_t TM_CCPCounter;        

} TM_CCPInitTypeDef;

/********* Select CCP Channel *************/

#define TM_CCP									((uint16_t)0x0000)	               //捕捉信号由CPP引脚输入	
#define TM_ILRC             					TM1_CR_CCPSEL                      //捕捉信号选择内部ILRC
#define IS_TM_CCPChanneL(Channel) (((Channel)==TM_CCP )||((Channel)==TM_ILRC )) 
 	
/****************Select CCP Counter**********************/

#define TM_CCPCounter1							((uint16_t)0x0000)		
#define TM_CCPCounter4            				TM1_CR_CCPM_0
#define TM_CCPCounter8             				TM1_CR_CCPM_1
#define TM_CCPCounter16           				((uint16_t)0x3000)
#define TM_CCPCounter32              			TM1_CR_CCPM_2
#define TM_CCPCounter64             			((uint16_t)0x5000)
#define TM_CCPCounter128            			((uint16_t)0x6000)
#define TM_CCPCounter256  						TM1_CR_CCPM

#define IS_TM_CCPCounter(Counter) (((Counter)==TM_CCPCounter1)||\
								  ((Counter)==TM_CCPCounter4)||\
								  ((Counter)==TM_CCPCounter8)||\
								  ((Counter)==TM_CCPCounter16)||\
								  ((Counter)==TM_CCPCounter32)||\
								  ((Counter)==TM_CCPCounter64)||\
								  ((Counter)==TM_CCPCounter128)||\
								  ((Counter)==TM_CCPCounter256)) 

/****************Select CCP Trigger**********************/

#define TM_CCPTrigger_Rising  				    ((uint16_t)0x0000)
#define TM_CCPTrigger_Falling  				    TM1_CR_CCPM_3
#define IS_TM_CCP_Trigger(Trigger) (((Trigger)==TM_CCPTrigger_Rising)||	((Trigger)==TM_CCPTrigger_Falling))							  

/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */ 

/* TimeBase management ********************************************************/

void TM_DeInit(TM_TypeDef* TMx);                            //定时器相关寄存器恢复成上电默认值

void TM_SetAutoReload(TM_TypeDef* TMx,uint16_t Value);      //设定时器的重载值 
void TM_Cmd(TM_TypeDef* TMx, FunctionalState NewState);     //使能定时器功能
uint16_t TM_GetCounter(TM_TypeDef* TMx);                    //获取定时器当前计数值

void TM_CCPConfig(uint16_t CCP_Channel,uint16_t TM_CCPTrigger,uint16_t TM_CCPCounterx);		//配置定时器1捕捉功的相关参数
void TM_CCPCmd(FunctionalState NewState);					                                //使能定时器1的捕捉功能	
uint16_t TM_GetCCPData(void);                                                               //获取定时器1的捕捉结果

void TM_PFDCmd(FunctionalState NewState);                                                   //使能定时器2的PFD功能，  注：使能PFD功能时，请提前配置好定时器2

/********************中断************************/
void TM_ITCmd(TM_TypeDef* TMx,FunctionalState NewState);                                    //使能定时器中断功能
void TM_CCPITCmd(FunctionalState NewState);                                                 //使能定时器1捕捉中断功能

ITStatus TM_GetITStatus(TM_TypeDef* TMx);                                                   //获取定时器中断标志位
void TM_ClearITStatus(TM_TypeDef* TMx);                                                     //清除定时器中断标志位

ITStatus TM_GetCCPITStatus(void);                                                           //获取定时器1捕捉中断标位
void TM_ClearCCPITStatus(void);                                                             //清除定时器1捕捉中断标位
    

#ifdef __cplusplus
}
#endif

#endif /* __SD93F115B_TIM_H */

/*****************************END OF FILE********************************/